package Kuaishou;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2021-06-16
 */
public class Test {
    public static void main(String[] args) {
        int[] array={1,2,2,3,3,3,6,6,8,10};
        System.out.println(new Test().GetNumberOfK(array, 3));
    }

    public int GetNumberOfK(int [] array , int k) {
        int left = getLeft(array,k) + 1;
        if(left > array.length){
            return 0;
        }
        if(array[left] != k){
            return 0;
        }
        int right = getRight(array,k) -1;
        System.out.println(left);
        System.out.println(right);
        return right - left + 1;

    }

    public int getLeft(int [] array , int k){
        int i = 0, j = array.length-1;
        while(i <= j){
            int m = (i + j)/2;
            if(array[m] >= k){
                j = m -1;
            }else{
                i = m+ 1;
            }
        }
        return j;
    }

    public int getRight(int [] array , int k){
        int i = 0, j = array.length-1;
        while(i <= j){
            int m = (i + j)/2;
            if(array[m] <= k){
                i = m+ 1;
            }else{
                j = m -1;
            }
        }
        return j;
    }
}
